﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents.DocumentStructures;
using System.Windows.Navigation;

namespace CADTool.Jig
{
    public class ArrayJig : DrawJig
    {
        private Point3d jigPoint;
        private Point3d basePoint;
        private List<Entity> entList;
        private List<Entity> ents;
        private int num;
        public ArrayJig(Point3d basePoint, List<Entity> entList, int num)
        {
            this.basePoint = basePoint;
            this.entList = entList;
            this.num = num;
        }
        public List<Entity> GetEntities()
        {
            return ents;
        }
        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            JigPromptPointOptions jigppo = new JigPromptPointOptions("\n请指定第二点");
            jigppo.Cursor = CursorType.RubberBand;
            jigppo.BasePoint = basePoint;
            jigppo.UseBasePoint = true;
            PromptPointResult ppr = prompts.AcquirePoint(jigppo);
            if (ppr.Status == PromptStatus.Cancel)
            {
                return SamplerStatus.Cancel;
            }
            else if (ppr.Status == PromptStatus.OK)
            {
                Point3d temp = ppr.Value;
                if (temp != basePoint)
                {
                    jigPoint = temp;
                    return SamplerStatus.OK;
                }
            }
            return SamplerStatus.NoChange;

        }      
        protected override bool WorldDraw(WorldDraw draw)
        {
            //克隆实体
            ents = new List<Entity>();        
            for (int i = 0; i < entList.Count; i++)
            {
                for (int j = 0; j < num; j++)
                {
                    Entity cloneEnt = entList[i].Clone() as Entity;
                    ents.Add(cloneEnt);
                }
            }
            Vector3d vec = basePoint.GetVectorTo(jigPoint);
            int js = 0;
            for (int i = 0; i < entList.Count; i++)
            {
                for (int j = 0; j < num; j++)
                {
                    ents[js].TransformBy(Matrix3d.Displacement(vec * j));
                    js++;
                }
            }
            foreach (var item in ents)
            {
                draw.Geometry.Draw(item);
            }
            return true;
        }
    }
}
